package ua.maxicash.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ua.maxicash.service.AppService;
import ua.maxicash.service.CalendarService;
import ua.maxicash.vo.SystemUser;
import ua.maxicash.vo.Work;
import ua.maxicash.vo.Worker;
import ua.maxicash.vo.WorkingDay;
import ua.maxicash.vo.WorkingDay.WorkingDayLight;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/calendar")
public class CalendarController {

    @Autowired
    CalendarService calendarService;

    @Autowired
    AppService appService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getCalendar(
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "year", required = false) Integer year) {

        ModelAndView mav = new ModelAndView("Calendar");
        return mav;

    }

    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/getData")
    @ResponseBody
    public List<Date> getCalendarContent(@RequestParam(value = "month", required = false) Integer month,
                                         @RequestParam(value = "year", required = false) Integer year) {

        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        SystemUser su = appService.getSystemUserByEmail(user.getUsername());

        return calendarService.getCalendarForMonth(month, year, su);

    }

    @RequestMapping("/daySchedule")
    public ModelAndView getDaySchedule(
            @RequestParam(value = "day", required = false) Integer day,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "year", required = false) Integer year) {

        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        SystemUser su = appService.getSystemUserByEmail(user.getUsername());

        Set<WorkingDay> wdays = calendarService.getCalendarForDay(day, month, year, su);

        ModelAndView mav = new ModelAndView("daySchedule");

        List<WorkingDayLight> wdayslight = new ArrayList<WorkingDayLight>();

        for (WorkingDay wd : wdays) {
            wdayslight.add(wd.getWorkingDayLight());
        }

        mav.addObject("wdayslight", wdayslight);

        return mav;

    }

    @PreAuthorize("hasRole('ROLE_WORKER')")
    @RequestMapping(value = "/getNearestWorkerWorks", method = RequestMethod.GET)
    @ResponseBody
    public Set<Work> getNearestWorkerWorks(
            @RequestParam("amount") int amount,
            HttpServletRequest req, HttpServletResponse resp) {

        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        SystemUser su = appService.getSystemUserByEmail(user.getUsername());
        Set<Work> works = calendarService.getNearestWorks(su, amount);

//        List<String> worksString = new ArrayList<String>();
//        for(Work work : works) {
//            worksString.add(work.toJSON());
//        }

        return works;
    }

    @PreAuthorize("hasRole('ROLE_WORKER')")
    @RequestMapping(value = "/getRandomWorks", method = RequestMethod.GET)
    @ResponseBody
    public Set<Work> getRandomWorks(
            @RequestParam(value = "amount", defaultValue = "0") int amount,
            HttpServletRequest req, HttpServletResponse resp) {

        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        SystemUser su = appService.getSystemUserByEmail(user.getUsername());
        Set<Work> works = calendarService.getRandomWorks(su, amount);

//        List<String> worksString = new ArrayList<String>();
//        for(Work work : works) {
//            worksString.add(work.toJSON());
//        }

        return works;
    }

}
